import {createSlice} from "@reduxjs/toolkit";
import {useSelector} from "react-redux";

const goodSlice = createSlice({
    name: "good",
    initialState: {
        goodList: [
            {
                id: 1,
                goodName: "苹果手机",
                goodsNum: 0
            }
        ]
    },
    reducers: {
        addGoodsNum(state, action) {
            state.goodList[0].goodsNum += action.payload;
            // console.log('QQQQ 商品的数量', store.getState());
        }
    }
})
// 将仓库的值暴露出去
// useSelector不能直接使用，必须转换成自定义hook
// 而自定义hook也必须遵守hook的规则，必须是use开头
// state.片段的name name是片段的唯一标识
export const useGoodSelector = () => useSelector(state => state.good)
export const {addGoodsNum} = goodSlice.actions;
export default goodSlice.reducer;
